const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');
const QQMapWX = require('../../utils/qqmap-wx-jssdk.js')
import * as echarts from '../../ec-canvas/echarts';
var app = getApp();
var qqMapSDK;

var list = []
var list1 = []


Page({
  data: {
    index: 0, //签收状态
    imgs: [], //本地图片
    images: [], //上传后的图片
    reason: '', //异常原因
    goods: '', //商品列表
    taskNumber: '', //任务号
    receiverPhone: '', //手机号
    array: ['签收完成', '部分签收', '拒绝签收'], //0=签收完成1=部分签收2=拒绝签收
    type: 1 //1收货签收 2发货签收
  },

  onLoad: function (options) {
    this.setData({
      vehicleId: options.id,
      goods: options.goods,
      taskNumber: options.taskNumber,
      receiverPhone: options.receiverPhone,
      type: options.type
    })

    if(options.type==2){
      wx.setNavigationBarTitle({
        title: '发货签收',
      })
    }

  },

  onShow: function () {},

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: parseInt(e.detail.value)
    })
  },

  reasonnInput: function (e) {
    this.setData({
      reason: e.detail.value
    })
  },

  //提交
  submit: function () {
    if (this.data.type == 1) { //收货签收
      this.deliveryOnlineSignFor()
    } else { //发货签收
      this.deliveryOnlineSignFor()
    }
  },

  //选择图片
  chooseImg: function (e) {

    if (this.data.imgs.length >= 4) {
      wx.showToast({
        title: '最多上传4张图片',
      })
      return
    }
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res.tempFilePaths[0])
        wx.uploadFile({
          filePath: res.tempFilePaths[0],
          name: 'file',
          url: api.updateImage + wx.getStorageSync('id'),
          success(res) {
            let imgTem = that.data.imgs
            let result = JSON.parse(res.data)
            console.log(result.data)
            imgTem.push(api.baseUrl + result.data)
            that.setData({
              imgs: imgTem
            })
          }
        })
      }
    })
  },

  //到货签收
  deliveryOnlineSignFor: function () {

    if (this.data.reason == '') {
      wx.showToast({
        title: '请输入破损原因',
      })
      return
    }

    let data = {
      abnormalCause: this.data.reason,
      images: this.data.imgs,
      signState: this.data.index,
      taskNumber: this.data.taskNumber,
      signPhone: this.data.receiverPhone,
      goodLists: this.data.goods
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.deliveryOnlineSignFor,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          wx.showToast({
            title: '已提交',
          })
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


  //到货签收
  deliveryOnlineSignFor: function () {

    if (this.data.reason == '') {
      wx.showToast({
        title: '请输入破损原因',
      })
      return
    }

    let data = {
      uid: wx.getStorageSync('id'),
      abnormalCause: this.data.reason,
      images: this.data.imgs,
      signState: this.data.index,
      waybillNumber: this.data.taskNumber,
      signPhone: this.data.receiverPhone,
      goodLists: this.data.goods
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.deliverySignFor,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          wx.showToast({
            title: '已提交',
          })
          wx.hideLoading()
         
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


})